<template>
  <el-dialog
    :title="!dataForm.roomsId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="房间号" prop="roomNumber">
      <el-input v-model="dataForm.roomNumber" placeholder="房间号"></el-input>
    </el-form-item>
    <el-form-item label="房间名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="房间名称"></el-input>
    </el-form-item>
    <el-form-item label="楼层" prop="floor">
      <el-select v-model="dataForm.floor" placeholder="请选择楼层">
        <el-option
          v-for="item in floorNameList"
          :key="item.id"
          :label="item.roomfloor"
          :value="item.roomfloor">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="房间面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="房间面积"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          roomsId: 0,
          roomNumber: '',
          name: '',
          floor: '',
          area: '',
          creator: '',
          createTime: '',
          updater: '',
          updateTime: '',
          deleted: ''
        },
        floorNameList:[

        ],
        dataRule: {
          roomNumber: [
            { required: true, message: '房间号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '房间名称不能为空', trigger: 'blur' }
          ],
          floor: [
            { required: true, message: '楼层不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '房间面积不能为空', trigger: 'blur' }
          ],
          creator: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updater: [
            { required: true, message: '更新人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },

    //界面完成加载之前发送请求数据
    created(){
       this.getFloorNameList();
    },


    methods: {
      getFloorNameList(){
        this.$http({
              url: this.$http.adornUrl(`/room/room/floorNameList`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.floorNameList=data.floorNameList
                console.log(data.floorNameList)
              }
            })
      },
      init (id) {
        this.dataForm.roomsId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.roomsId) {
            this.$http({
              url: this.$http.adornUrl(`/rooms/rooms/info/${this.dataForm.roomsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.roomNumber = data.rooms.roomNumber
                this.dataForm.name = data.rooms.name
                this.dataForm.floor = data.rooms.floor
                this.dataForm.area = data.rooms.area
                this.dataForm.creator = data.rooms.creator
                this.dataForm.createTime = data.rooms.createTime
                this.dataForm.updater = data.rooms.updater
                this.dataForm.updateTime = data.rooms.updateTime
                this.dataForm.deleted = data.rooms.deleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rooms/rooms/${!this.dataForm.roomsId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'roomsId': this.dataForm.roomsId || undefined,
                'roomNumber': this.dataForm.roomNumber,
                'name': this.dataForm.name,
                'floor': this.dataForm.floor,
                'area': this.dataForm.area,
                'creator': this.dataForm.creator,
                'createTime': this.dataForm.createTime,
                'updater': this.dataForm.updater,
                'updateTime': this.dataForm.updateTime,
                'deleted': this.dataForm.deleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
